package com.xh.bussiness.exam.controller;

import com.xh.bussiness.exam.bean.request.QueryStudentPaperAnswerRequest;
import com.xh.bussiness.exam.bean.request.SubmitEvaluationResultDto;
import com.xh.bussiness.exam.model.StudentPaperAnswer;
import com.xh.bussiness.exam.bean.request.StudentPaperAnswerRequest;
import com.xh.bussiness.exam.service.IStudentPaperAnswerService;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Pitt Cheung
 * @since 2021-07-28
 */
@RestController
@RequestMapping("/admin/studentPaperAnswer")
public class StudentPaperAnswerController extends BaseController {

    @Autowired
    private IStudentPaperAnswerService studentPaperAnswerService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, QueryStudentPaperAnswerRequest bean){
        page=studentPaperAnswerService.queryStudentPaperAnswerPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(StudentPaperAnswerRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",studentPaperAnswerService.queryStudentPaperAnswer(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(StudentPaperAnswerRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=studentPaperAnswerService.exportAll(request,exportHeaderBeans,bean,"queryStudentPaperAnswerPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param studentPaperAnswer 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated StudentPaperAnswer studentPaperAnswer){
        if(studentPaperAnswer.getId()==null){
            studentPaperAnswerService.insert(studentPaperAnswer);
        }else{
            studentPaperAnswerService.update(studentPaperAnswer);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        studentPaperAnswerService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        studentPaperAnswerService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation("小程序提价测评")
    @PostMapping("/submitEvaluationResult")
    @Transactional
    public AjaxResult submitEvaluationResult(@RequestBody SubmitEvaluationResultDto request) {
        String checkedMsg = request.checkVal();
        if (!StringUtils.isEmpty(checkedMsg)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, checkedMsg);
        }
        studentPaperAnswerService.submitEvaluationResult(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation("查询测评作答结果")
    @RequestMapping("/queryStudentPaperAnswer")
    public AjaxResult queryStudentPaperAnswer(String studentId, String paperId) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, studentPaperAnswerService.queryStudentPaperAnswer(studentId, paperId
        ));
    }


    @ApiOperation("小程序-进门测-学情分析报告")
    @RequestMapping("/queryPaperTestStudentAnalysisInfoReport")
    public AjaxResult queryPaperTestStudentAnalysisInfoReport(String studentId, String paperId) {
        if (StringUtils.isEmpty(studentId) || StringUtils.isEmpty(paperId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, studentPaperAnswerService.queryPaperTestStudentAnalysisInfoReport(studentId, paperId));
    }

    @ApiOperation("小程序-进门测-知识点分析报告")
    @RequestMapping("/queryPaperTestStudentKnowledgeAnalysisReport")
    public AjaxResult queryPaperTestStudentKnowledgeAnalysisReport(String studentId, String paperId) {
        if (StringUtils.isEmpty(studentId) || StringUtils.isEmpty(paperId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, studentPaperAnswerService.queryPaperTestStudentKnowledgeAnalysisReport(studentId, paperId));
    }

    @ApiOperation("发送报告")
    @RequestMapping("/sendStudentReport")
    public AjaxResult sendStudentReport(String studentAnswerId) {
        if (StringUtils.isEmpty(studentAnswerId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        StudentPaperAnswer byId = studentPaperAnswerService.getById(studentAnswerId);
        if (byId == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "作答结果不存在");
        }
        byId.setReportStatus(1);
        studentPaperAnswerService.update(byId);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

}

